﻿using System;
using System.Collections.Generic;
using System.Text;
using VersionOne.SDK.ObjectModel;
using ProjectManagement.TFS;
using Microsoft.TeamFoundation.Client;
using System.Net;
using WorkItem = Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem;

namespace ProjectManagement.VersionOne
{
    /// <summary>
    /// sync V1 and TFS tasks
    /// </summary>
    public class V1TfsTaskSyncUtil
    {
        public void SyncV1TasksToTfsTasks(
            string tfServerName, string tfProjName, DateTime lastSyncTime,
            string v1ServerName, string v1ProjName, string v1UserName, string v1Password)
        {
            List<Task> v1Tasks = 
                this.GetNewTasksFromVersionOne(v1ServerName, v1ProjName, v1UserName, 
                v1Password, lastSyncTime);
            if (v1Tasks != null && v1Tasks.Count > 0)
            {
                foreach (Task v1Task in v1Tasks)
                {
                    this.CreateTfsTask(
                        tfServerName, tfProjName, Environment.UserName,
                        v1Task.Name, v1Task.Description,
                        v1Task.CreateDate, new TimeSpan((int)v1Task.ToDo, 0, 0));
                }
            }
        }

        #region TFS
        public void CreateTfsTask(string tfServerName, string tfProjName, string owner,
            string taskName, string desc, DateTime startTime, TimeSpan duration)
        {
            TeamFoundationServer tfs = new TeamFoundationServer(tfServerName);
            try
            {
                tfs.Authenticate();
            }
            catch
            {
                tfs =
                    new TeamFoundationServer(tfServerName, new NetworkCredential("Administrator", "xiaodong", "server"));
            }
            TfsUtility.CreateTask(tfs, tfProjName, taskName, desc, owner, startTime, duration);
        }

        public List<WorkItem> GetActiveTfsWorkItems(string tfServerName, string tfProjName)
        {
            TeamFoundationServer tfs = new TeamFoundationServer(tfServerName);
            try
            {
                tfs.Authenticate();
            }
            catch
            {
                tfs =
                    new TeamFoundationServer(tfServerName, new NetworkCredential("Administrator", "xiaodong", "server"));
            }
            TfsUtility.GetActiveProjectTasks(tfs, tfProjName, Environment.UserName);
            return TfsUtility.GetActiveProjectTasks(tfs, tfProjName, Environment.UserName);
        }
            
        #endregion

        #region V1
        public List<Task> GetNewTasksFromVersionOne(string v1ServerName, string v1ProjName,
            string owner, string password, DateTime timestamp)
        {
            V1Instance v1 = V1Util.GetV1Instance(v1ServerName, owner, password);
            Member v1Member = V1Util.GetV1Member(v1, owner);
            Project v1proj = V1Util.GetV1Project(v1, v1ProjName);
            ICollection<Task> projTasks = V1Util.GetProjectTasks(v1proj);
            List<Task> tasks = new List<Task>();
            foreach (Task v1Task in projTasks)
            {
                if (v1Task.CreateDate >= timestamp)
                {
                    if (v1Task.Owners.Contains(v1Member))
                    {
                        tasks.Add(v1Task);
                    }
                }
            }
            return tasks;
        }

        public List<Task> GetNewTasksFromVersionOne(string v1ServerName, string v1ProjName,
            string owner, string password)
        {
            V1Instance v1 = V1Util.GetV1Instance(v1ServerName, owner, password);
            Member v1Member = V1Util.GetV1Member(v1, owner);
            Project v1proj = V1Util.GetV1Project(v1, v1ProjName);
            ICollection<Task> projTasks = V1Util.GetProjectTasks(v1proj);
            List<Task> tasks = new List<Task>();
            foreach (Task v1Task in projTasks)
            {
                if (v1Task.Owners.Contains(v1Member))
                {
                    tasks.Add(v1Task);
                }
            }
            return tasks;
        }
        #endregion
    }
}
